<?php

// 课程信息控制器

namespace Home\Controller;
use Think\Controller;


class CourseController extends PublicController {

	// 查看课程列表
    public function showList() {
    	$class_id = I('get.class_id', 1); // 从地址栏获取班级所属ID值 ,如无则默认为1

    	// 班级信息
    	$class_info = M('class')->select();


    	$this->assign('class_id', $class_id);
    	$this->assign('class_info', $class_info);
    	$this->display();
    }

    // 录入课程信息
    public function addInfo() {

    	// 班级信息
    	$class_info = M('class')->select();

    	// 课程信息
    	$course_info = M('course')->select();

        if( IS_POST ) {
            $data = I('post.');
            // 检测是否已经存在课程信息, 如有 则修改原有课程, 如无 则添加新课程
            $info = M('course_arrangement')->field('id')->where('class_id = %d and week_id = %d and part_id = %d', array($data['class_id'], $data['week_id'], $data['part_id']))->find();
            if( !empty($info) ) { // 执行修改功能
                $id = $info['id'];
                $rs = M('course_arrangement')->where('id = %d', $id)->save($data);
            } else { // 执行添加功能
                $rs = M('course_arrangement')->add($data);
            }
            
            if( $rs ) { // 成功
                $this->success('操作成功', '', 3);
            } else { // 失败
                $this->error('操作失败', '', 3);
            }
            exit;
        }


    	$this->assign('class_info', $class_info);
    	$this->assign('course_info', $course_info);

    	$this->display();
    }

}